﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace RohonTool.Win32Helper
{
    public class ConsoleWin32Helper
    {
        private string title;
        public ConsoleWin32Helper(string title)
        {
            _NotifyIcon.Icon = new Icon(@"images/103.ico");
            _NotifyIcon.Visible = false;
            this.title = title;
            _NotifyIcon.Text = title;
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "右键菜单，还没有添加事件";
            item.Index = 0;
            menu.MenuItems.Add(item);
            _NotifyIcon.ContextMenu = menu;
            _NotifyIcon.MouseDoubleClick += new MouseEventHandler(_NotifyIcon_MouseDoubleClick);
            Hidden();
        }
        /// <summary>
        /// 托盘双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void _NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //RohonOutPut.OutPut.Report.OutPut("托盘被双击.");
            int a = new Random().Next(5);
            //Console.WriteLine(a.ToString());
            if (a > 2)
            {
                Show();
            }
            else
            {
                Hidden();
            }

        }

        #region 调用一些Win32的窗口操作方法
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
        [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
        [DllImport("User32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(IntPtr hwind, int cmdShow);
        [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hwind);
        #endregion


        #region 禁用关闭按钮
        ///<summary>
        /// 禁用关闭按钮
        ///</summary>
        ///<param name="consoleName">控制台名字</param>
        public void DisableCloseButton(string title)//线程睡眠，确保closebtn中能够正常FindWindow，否则有时会Find失败。。  
        {
            Thread.Sleep(100);
            IntPtr windowHandle = FindWindow(null, title);
            IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
            uint SC_CLOSE = 0xF060;
            RemoveMenu(closeMenu, SC_CLOSE, 0x0);
        }
        #endregion

        #region 显示或隐藏面板
        public bool IsExistsConsole(string title)
        {
            IntPtr windowHandle = FindWindow(null, title);
            if (windowHandle.Equals(IntPtr.Zero))
            {
                return false;
            }
            return true;
        }

        public void Hidden()
        {
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = FindWindow(null, title);
            int normalState = 0;//窗口状态(隐藏)
            ShowWindow(ParenthWnd, normalState);
        }

        public void Show()
        {
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = FindWindow(null, title);
            int normalState = 9;//窗口状态(隐藏)
            ShowWindow(ParenthWnd, normalState);
        }
        #endregion


        #region 托盘图标
        static NotifyIcon _NotifyIcon = new NotifyIcon();
        public void ShowNotifyIcon()
        {
            _NotifyIcon.Visible = true;
            //  _NotifyIcon.ShowBalloonTip(3000, "", "我是托盘图标，用右键点击我试试，还可以双击看看。", ToolTipIcon.None);
        }
        public void HideNotifyIcon()
        {
            _NotifyIcon.Visible = false;
        }
        #endregion


    }
}
